project(LiveGraph)

include_directories(../Src/Ui)
include_directories(../Src/Protocol)
include_directories(../Src/Interface)
set(EXE_SOURCES
        main.cpp
        ../Src/Ui/LiveViewWindow.cpp
        ../Src/Interface/SerialPortHandler.cpp
        ../Src/Protocol/LiveProtocol.cpp
        ../Src/Protocol/comm_crc.c
        ../Src/Protocol/comm_protocol.h
        ../Src/CommTools/RingBuffer.cpp
        ../Src/Ui/LiveView.ui
        )
qt5_add_resources(EXE_SOURCES Res/icon.qrc)
message("src:${EXE_SOURCES}")
add_executable(LiveGraph
        ${EXE_SOURCES}
        )
message("qwt library:${QWT_LIBRARY}")
target_link_libraries(LiveGraph
        glog::glog
        Qt5::Core
        Qt5::Gui
        Qt5::Widgets
        Qt5::Svg
        Qt5::SerialPort
        ${QWT_LIBRARY}
        )

if (WIN32)
    set(QT_INSTALL_PATH  ${Qt5_DIR}/../../../)
    if (NOT EXISTS "${QT_INSTALL_PATH}/bin")
        set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..")
        if (NOT EXISTS "${QT_INSTALL_PATH}/bin")
            set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..")
        else ()
            break()
        endif ()
    elseif(EXISTS "${QT_INSTALL_PATH}/debug")
        break()
    endif ()
    message("Qt is installed on:${QT_INSTALL_PATH}")
    if (EXISTS "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll")
        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E make_directory
                "$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/platforms/")
        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E copy
                "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll"
                "$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/platforms/")
    endif ()
    message("copy qt runtime to project")
    foreach (QT_LIB Core Gui Svg Widgets OpenGL SerialPort)
        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E copy
                "${QT_INSTALL_PATH}/bin/Qt5${QT_LIB}${DEBUG_SUFFIX}.dll"
                "$<TARGET_FILE_DIR:${PROJECT_NAME}>")
    endforeach (QT_LIB)
    message("copy qwt.dll to project")
    add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy
    "${QT_INSTALL_PATH}/bin/qwt.dll"
    "$<TARGET_FILE_DIR:${PROJECT_NAME}>")
endif ()